// JavaScript Document
function inputSudoku(row, col, input)
{
	//
	input.value = input.value.replace(/\D/g, '');
	input.value = input.value.replace(/0/g, '');

	var input_name = "input" + row + col;
	var len = input.value.length;
	var n = 0;

	// 根据不同长度，不同的处理
	if (len == 0)
	{
		n = 0;
		document.getElementById(input_name).className = "input_0";
	}
	else if (len == 1)
	{
		n = parseInt(input.value);
		if (isNaN(n)) {
			n = 0;
		}
		document.getElementById(input_name).className = "input_1";
	}
	else if (len == 2)
	{
		// 检查同一数字输多次
		if (input.value.charAt(1) == input.value.charAt(0))		// 两数字相同
		{
			input.value = input.value.substr(0, 1);
			len = 1;
			if (document.getElementById(input_name).className == "input_1")
			{
				n = 0;
				document.getElementById(input_name).className = "input_1_small";
			}
			else
			{
				n = parseInt(input.value);
				if (isNaN(n)) {
					n = 0;
				}
				document.getElementById(input_name).className = "input_1";
			}
		}
		else	// 两数字不同
		{
			n = 0;
			document.getElementById(input_name).className = "input_2";
		}
	}
	else if (len == 3)
	{
		// 检查同一数字输入多次
		if ((input.value.charAt(2) == input.value.charAt(0)) || (input.value.charAt(2) == input.value.charAt(1)))
		{
			input.value = input.value.substr(0, 2);
			len = 2;
			n = 0;
			document.getElementById(input_name).className = "input_2";
		}
		else
		{
			n = 0;
			document.getElementById(input_name).className = "input_3";
		}
	}
	else
	{
		n = 0;
	}

	//
	sudoku[row][col] = n;

	// 先将本行，本列，本块置为绿色
	var block = calBlock(row, col);

	notifyRow(row, false);
	notifyCol(col, false);
	notifyBlock(block, false);

	var has_error = 0;

	// 检查所有行
	for (r = 1; r <= 9; r++)
	{
		rc = checkRow(r);
		if (false == rc) {
			notifyRow(r, true);
			has_error = 1;
		}
	}

	// 检查所有列
	for (c = 1; c <= 9; c++)
	{
		rc = checkCol(c);
		if (false == rc) {
			notifyCol(c, true);
			has_error = 1;
		}
	}

	// 检查当前块
	rc = checkBlock(block);
	if (false == rc) {
		notifyBlock(block, true);
		has_error = 1;
	}

	// 检查是否完成
	if (0 == has_error)
	{
		setTimeout("checkComplete();", 1);
	}
}

function checkComplete()
{
	var i = 0;

	// 检查是否填写完毕
	for (i = 1; i <= 9; i++)
	{
		for (j = 1; j <= 9; j++)
		{
			if (0 == sudoku[i][j]) {
				return;
			}
		}
	}

	// 填写完毕, 检查是否完成
	for (i = 1; i <= 9; i++)
	{
		rc = checkRow(i);
		if (false == rc) {
			return;
		}
	}

	for (i = 1; i <= 9; i++)
	{
		rc = checkCol(i);
		if (false == rc) {
			return;
		}
	}

	for (i = 1; i <= 9; i++)
	{
		rc = checkBlock(i);
		if (false == rc) {
			return;
		}
	}

	// 游戏完成, 提交
	var result = "";
	for (i = 1; i <= 9; i++)
	{
		for (j = 1; j <= 9; j++)
		{
			result += sudoku[i][j];
		}
	}

	document.getElementById('info_frame').contentWindow.document.getElementById('sudoku_result').ID.value = ID;
	document.getElementById('info_frame').contentWindow.document.getElementById('sudoku_result').user.value = user;
	document.getElementById('info_frame').contentWindow.document.getElementById('sudoku_result').sudoku_session.value = sudoku_session;
	document.getElementById('info_frame').contentWindow.document.getElementById('sudoku_result').result.value = result;
	document.getElementById('info_frame').contentWindow.document.getElementById('sudoku_result').submit();

	//改用弹出窗口提示
	//document.getElementById('success').innerText = "恭喜你,成功:)"
	alert("恭喜您已经成功解答了这个数独迷题!");
	inputs = document.getElementById("game_table1").getElementsByTagName("input");
	for(i=0;i<inputs.length;i++)
	{
		inputs[i].disabled = true;
		inputs[i].style.textAlign = "center";
	}
	return;
}

function calBlock(row, col)
{
	var block_row = 0;
	var block_col = 0;

	if (row <= 3) {
		block_row = 1;
	}
	else if (row <= 6) {
		block_row = 2;
	}
	else {
		block_row = 3;
	}

	if (col <= 3) {
		block_col = 1;
	}
	else if (col <= 6) {
		block_col = 2;
	}
	else {
		block_col = 3;
	}

	var block = (block_row - 1) * 3 + block_col;
	return block;
}

function checkRow(n)
{
	var rc = false;
	switch (n)
	{
	case 1:
		rc = check(sudoku[1][1], sudoku[1][2], sudoku[1][3], sudoku[1][4], sudoku[1][5], sudoku[1][6], sudoku[1][7], sudoku[1][8], sudoku[1][9]);
		break;

	case 2:
		rc = check(sudoku[2][1], sudoku[2][2], sudoku[2][3], sudoku[2][4], sudoku[2][5], sudoku[2][6], sudoku[2][7], sudoku[2][8], sudoku[2][9]);
		break;

	case 3:
		rc = check(sudoku[3][1], sudoku[3][2], sudoku[3][3], sudoku[3][4], sudoku[3][5], sudoku[3][6], sudoku[3][7], sudoku[3][8], sudoku[3][9]);
		break;

	case 4:
		rc = check(sudoku[4][1], sudoku[4][2], sudoku[4][3], sudoku[4][4], sudoku[4][5], sudoku[4][6], sudoku[4][7], sudoku[4][8], sudoku[4][9]);
		break;

	case 5:
		rc = check(sudoku[5][1], sudoku[5][2], sudoku[5][3], sudoku[5][4], sudoku[5][5], sudoku[5][6], sudoku[5][7], sudoku[5][8], sudoku[5][9]);
		break;

	case 6:
		rc = check(sudoku[6][1], sudoku[6][2], sudoku[6][3], sudoku[6][4], sudoku[6][5], sudoku[6][6], sudoku[6][7], sudoku[6][8], sudoku[6][9]);
		break;

	case 7:
		rc = check(sudoku[7][1], sudoku[7][2], sudoku[7][3], sudoku[7][4], sudoku[7][5], sudoku[7][6], sudoku[7][7], sudoku[7][8], sudoku[7][9]);
		break;

	case 8:
		rc = check(sudoku[8][1], sudoku[8][2], sudoku[8][3], sudoku[8][4], sudoku[8][5], sudoku[8][6], sudoku[8][7], sudoku[8][8], sudoku[8][9]);
		break;

	case 9:
		rc = check(sudoku[9][1], sudoku[9][2], sudoku[9][3], sudoku[9][4], sudoku[9][5], sudoku[9][6], sudoku[9][7], sudoku[9][8], sudoku[9][9]);
		break;

	default:
		rc = false;
		break;
	}

	return rc;
}

function notifyRow(n, show)
{
	var classname;
	if (true == show) {
		classname = "error";
	}
	else {
		classname = "normal";
	}

	switch (n)
	{
	case 1:
		document.getElementById('grid11').className = classname;
		document.getElementById('grid12').className = classname;
		document.getElementById('grid13').className = classname;
		document.getElementById('grid14').className = classname;
		document.getElementById('grid15').className = classname;
		document.getElementById('grid16').className = classname;
		document.getElementById('grid17').className = classname;
		document.getElementById('grid18').className = classname;
		document.getElementById('grid19').className = classname;
		break;

	case 2:
		document.getElementById('grid21').className = classname;
		document.getElementById('grid22').className = classname;
		document.getElementById('grid23').className = classname;
		document.getElementById('grid24').className = classname;
		document.getElementById('grid25').className = classname;
		document.getElementById('grid26').className = classname;
		document.getElementById('grid27').className = classname;
		document.getElementById('grid28').className = classname;
		document.getElementById('grid29').className = classname;
		break;

	case 3:
		document.getElementById('grid31').className = classname;
		document.getElementById('grid32').className = classname;
		document.getElementById('grid33').className = classname;
		document.getElementById('grid34').className = classname;
		document.getElementById('grid35').className = classname;
		document.getElementById('grid36').className = classname;
		document.getElementById('grid37').className = classname;
		document.getElementById('grid38').className = classname;
		document.getElementById('grid39').className = classname;
		break;

	case 4:
		document.getElementById('grid41').className = classname;
		document.getElementById('grid42').className = classname;
		document.getElementById('grid43').className = classname;
		document.getElementById('grid44').className = classname;
		document.getElementById('grid45').className = classname;
		document.getElementById('grid46').className = classname;
		document.getElementById('grid47').className = classname;
		document.getElementById('grid48').className = classname;
		document.getElementById('grid49').className = classname;
		break;

	case 5:
		document.getElementById('grid51').className = classname;
		document.getElementById('grid52').className = classname;
		document.getElementById('grid53').className = classname;
		document.getElementById('grid54').className = classname;
		document.getElementById('grid55').className = classname;
		document.getElementById('grid56').className = classname;
		document.getElementById('grid57').className = classname;
		document.getElementById('grid58').className = classname;
		document.getElementById('grid59').className = classname;
		break;

	case 6:
		document.getElementById('grid61').className = classname;
		document.getElementById('grid62').className = classname;
		document.getElementById('grid63').className = classname;
		document.getElementById('grid64').className = classname;
		document.getElementById('grid65').className = classname;
		document.getElementById('grid66').className = classname;
		document.getElementById('grid67').className = classname;
		document.getElementById('grid68').className = classname;
		document.getElementById('grid69').className = classname;
		break;

	case 7:
		document.getElementById('grid71').className = classname;
		document.getElementById('grid72').className = classname;
		document.getElementById('grid73').className = classname;
		document.getElementById('grid74').className = classname;
		document.getElementById('grid75').className = classname;
		document.getElementById('grid76').className = classname;
		document.getElementById('grid77').className = classname;
		document.getElementById('grid78').className = classname;
		document.getElementById('grid79').className = classname;
		break;

	case 8:
		document.getElementById('grid81').className = classname;
		document.getElementById('grid82').className = classname;
		document.getElementById('grid83').className = classname;
		document.getElementById('grid84').className = classname;
		document.getElementById('grid85').className = classname;
		document.getElementById('grid86').className = classname;
		document.getElementById('grid87').className = classname;
		document.getElementById('grid88').className = classname;
		document.getElementById('grid89').className = classname;
		break;

	case 9:
		document.getElementById('grid91').className = classname;
		document.getElementById('grid92').className = classname;
		document.getElementById('grid93').className = classname;
		document.getElementById('grid94').className = classname;
		document.getElementById('grid95').className = classname;
		document.getElementById('grid96').className = classname;
		document.getElementById('grid97').className = classname;
		document.getElementById('grid98').className = classname;
		document.getElementById('grid99').className = classname;
		break;

	default:
		rc = false;
		break;
	}
}

function checkCol(n)
{
	var rc = false;
	switch (n)
	{
	case 1:
		rc = check(sudoku[1][1], sudoku[2][1], sudoku[3][1], sudoku[4][1], sudoku[5][1], sudoku[6][1], sudoku[7][1], sudoku[8][1], sudoku[9][1]);
		break;

	case 2:
		rc = check(sudoku[1][2], sudoku[2][2], sudoku[3][2], sudoku[4][2], sudoku[5][2], sudoku[6][2], sudoku[7][2], sudoku[8][2], sudoku[9][2]);
		break;

	case 3:
		rc = check(sudoku[1][3], sudoku[2][3], sudoku[3][3], sudoku[4][3], sudoku[5][3], sudoku[6][3], sudoku[7][3], sudoku[8][3], sudoku[9][3]);
		break;

	case 4:
		rc = check(sudoku[1][4], sudoku[2][4], sudoku[3][4], sudoku[4][4], sudoku[5][4], sudoku[6][4], sudoku[7][4], sudoku[8][4], sudoku[9][4]);
		break;

	case 5:
		rc = check(sudoku[1][5], sudoku[2][5], sudoku[3][5], sudoku[4][5], sudoku[5][5], sudoku[6][5], sudoku[7][5], sudoku[8][5], sudoku[9][5]);
		break;

	case 6:
		rc = check(sudoku[1][6], sudoku[2][6], sudoku[3][6], sudoku[4][6], sudoku[5][6], sudoku[6][6], sudoku[7][6], sudoku[8][6], sudoku[9][6]);
		break;

	case 7:
		rc = check(sudoku[1][7], sudoku[2][7], sudoku[3][7], sudoku[4][7], sudoku[5][7], sudoku[6][7], sudoku[7][7], sudoku[8][7], sudoku[9][7]);
		break;

	case 8:
		rc = check(sudoku[1][8], sudoku[2][8], sudoku[3][8], sudoku[4][8], sudoku[5][8], sudoku[6][8], sudoku[7][8], sudoku[8][8], sudoku[9][8]);
		break;

	case 9:
		rc = check(sudoku[1][9], sudoku[2][9], sudoku[3][9], sudoku[4][9], sudoku[5][9], sudoku[6][9], sudoku[7][9], sudoku[8][9], sudoku[9][9]);
		break;

	default:
		rc = false;
		break;
	}

	return rc;
}

function notifyCol(n, show)
{
	var classname;
	if (true == show) {
		classname = "error";
	}
	else {
		classname = "normal";
	}
	
	switch (n)
	{
	case 1:
		document.getElementById('grid11').className = classname;
		document.getElementById('grid21').className = classname;
		document.getElementById('grid31').className = classname;
		document.getElementById('grid41').className = classname;
		document.getElementById('grid51').className = classname;
		document.getElementById('grid61').className = classname;
		document.getElementById('grid71').className = classname;
		document.getElementById('grid81').className = classname;
		document.getElementById('grid91').className = classname;
		break;

	case 2:
		document.getElementById('grid12').className = classname;
		document.getElementById('grid22').className = classname;
		document.getElementById('grid32').className = classname;
		document.getElementById('grid42').className = classname;
		document.getElementById('grid52').className = classname;
		document.getElementById('grid62').className = classname;
		document.getElementById('grid72').className = classname;
		document.getElementById('grid82').className = classname;
		document.getElementById('grid92').className = classname;
		break;

	case 3:

		document.getElementById('grid13').className = classname;
		document.getElementById('grid23').className = classname;
		document.getElementById('grid33').className = classname;
		document.getElementById('grid43').className = classname;
		document.getElementById('grid53').className = classname;
		document.getElementById('grid63').className = classname;
		document.getElementById('grid73').className = classname;
		document.getElementById('grid83').className = classname;
		document.getElementById('grid93').className = classname;
		break;

	case 4:
		document.getElementById('grid14').className = classname;
		document.getElementById('grid24').className = classname;
		document.getElementById('grid34').className = classname;
		document.getElementById('grid44').className = classname;
		document.getElementById('grid54').className = classname;
		document.getElementById('grid64').className = classname;
		document.getElementById('grid74').className = classname;
		document.getElementById('grid84').className = classname;
		document.getElementById('grid94').className = classname;
		break;

	case 5:
		document.getElementById('grid15').className = classname;
		document.getElementById('grid25').className = classname;
		document.getElementById('grid35').className = classname;
		document.getElementById('grid45').className = classname;
		document.getElementById('grid55').className = classname;
		document.getElementById('grid65').className = classname;
		document.getElementById('grid75').className = classname;
		document.getElementById('grid85').className = classname;
		document.getElementById('grid95').className = classname;
		break;

	case 6:
		document.getElementById('grid16').className = classname;
		document.getElementById('grid26').className = classname;
		document.getElementById('grid36').className = classname;
		document.getElementById('grid46').className = classname;
		document.getElementById('grid56').className = classname;
		document.getElementById('grid66').className = classname;
		document.getElementById('grid76').className = classname;
		document.getElementById('grid86').className = classname;
		document.getElementById('grid96').className = classname;
		break;

	case 7:
		document.getElementById('grid17').className = classname;
		document.getElementById('grid27').className = classname;
		document.getElementById('grid37').className = classname;
		document.getElementById('grid47').className = classname;
		document.getElementById('grid57').className = classname;
		document.getElementById('grid67').className = classname;
		document.getElementById('grid77').className = classname;
		document.getElementById('grid87').className = classname;
		document.getElementById('grid97').className = classname;
		break;

	case 8:
		document.getElementById('grid18').className = classname;
		document.getElementById('grid28').className = classname;
		document.getElementById('grid38').className = classname;
		document.getElementById('grid48').className = classname;
		document.getElementById('grid58').className = classname;
		document.getElementById('grid68').className = classname;
		document.getElementById('grid78').className = classname;
		document.getElementById('grid88').className = classname;
		document.getElementById('grid98').className = classname;
		break;

	case 9:
		document.getElementById('grid19').className = classname;
		document.getElementById('grid29').className = classname;
		document.getElementById('grid39').className = classname;
		document.getElementById('grid49').className = classname;
		document.getElementById('grid59').className = classname;
		document.getElementById('grid69').className = classname;
		document.getElementById('grid79').className = classname;
		document.getElementById('grid89').className = classname;
		document.getElementById('grid99').className = classname;
		break;

	default:
		rc = false;
		break;
	}
}

function checkBlock(n)
{
	var rc = false;

	switch (n)
	{
	case 1:
		rc = check(sudoku[1][1], sudoku[1][2], sudoku[1][3], sudoku[2][1], sudoku[2][2], sudoku[2][3], sudoku[3][1], sudoku[3][2], sudoku[3][3]);
		break;

	case 2:
		rc = check(sudoku[1][4], sudoku[1][5], sudoku[1][6], sudoku[2][4], sudoku[2][5], sudoku[2][6], sudoku[3][4], sudoku[3][5], sudoku[3][6]);
		break;

	case 3:
		rc = check(sudoku[1][7], sudoku[1][8], sudoku[1][9], sudoku[2][7], sudoku[2][8], sudoku[2][9], sudoku[3][7], sudoku[3][8], sudoku[3][9]);
		break;

	case 4:
		rc = check(sudoku[4][1], sudoku[4][2], sudoku[4][3], sudoku[5][1], sudoku[5][2], sudoku[5][3], sudoku[6][1], sudoku[6][2], sudoku[6][3]);
		break;

	case 5:
		rc = check(sudoku[4][4], sudoku[4][5], sudoku[4][6], sudoku[5][4], sudoku[5][5], sudoku[5][6], sudoku[6][4], sudoku[6][5], sudoku[6][6]);
		break;

	case 6:
		rc = check(sudoku[4][7], sudoku[4][8], sudoku[4][9], sudoku[5][7], sudoku[5][8], sudoku[5][9], sudoku[6][7], sudoku[6][8], sudoku[6][9]);
		break;

	case 7:
		rc = check(sudoku[7][1], sudoku[7][2], sudoku[7][3], sudoku[8][1], sudoku[8][2], sudoku[8][3], sudoku[9][1], sudoku[9][2], sudoku[9][3]);
		break;

	case 8:
		rc = check(sudoku[7][4], sudoku[7][5], sudoku[7][6], sudoku[8][4], sudoku[8][5], sudoku[8][6], sudoku[9][4], sudoku[9][5], sudoku[9][6]);
		break;

	case 9:
		rc = check(sudoku[7][7], sudoku[7][8], sudoku[7][9], sudoku[8][7], sudoku[8][8], sudoku[8][9], sudoku[9][7], sudoku[9][8], sudoku[9][9]);
		break;

	default:
		rc = false;
		break;
	}

	return rc;
}

function notifyBlock(n, show)
{
	var classname;
	if (true == show) {
		classname = "error";
	}
	else {
		classname = "normal";
	}
	
	switch (n)
	{
	case 1:
		document.getElementById('grid11').className = classname;
		document.getElementById('grid12').className = classname;
		document.getElementById('grid13').className = classname;
		document.getElementById('grid21').className = classname;
		document.getElementById('grid22').className = classname;
		document.getElementById('grid23').className = classname;
		document.getElementById('grid31').className = classname;
		document.getElementById('grid32').className = classname;
		document.getElementById('grid33').className = classname;
		break;

	case 2:
		document.getElementById('grid14').className = classname;
		document.getElementById('grid15').className = classname;
		document.getElementById('grid16').className = classname;
		document.getElementById('grid24').className = classname;
		document.getElementById('grid25').className = classname;
		document.getElementById('grid26').className = classname;
		document.getElementById('grid34').className = classname;
		document.getElementById('grid35').className = classname;
		document.getElementById('grid36').className = classname;
		break;

	case 3:
		document.getElementById('grid17').className = classname;
		document.getElementById('grid18').className = classname;
		document.getElementById('grid19').className = classname;
		document.getElementById('grid27').className = classname;
		document.getElementById('grid28').className = classname;
		document.getElementById('grid29').className = classname;
		document.getElementById('grid37').className = classname;
		document.getElementById('grid38').className = classname;
		document.getElementById('grid39').className = classname;
		break;

	case 4:
		document.getElementById('grid41').className = classname;
		document.getElementById('grid42').className = classname;
		document.getElementById('grid43').className = classname;
		document.getElementById('grid51').className = classname;
		document.getElementById('grid52').className = classname;
		document.getElementById('grid53').className = classname;
		document.getElementById('grid61').className = classname;
		document.getElementById('grid62').className = classname;
		document.getElementById('grid63').className = classname;
		break;

	case 5:
		document.getElementById('grid44').className = classname;
		document.getElementById('grid45').className = classname;
		document.getElementById('grid46').className = classname;
		document.getElementById('grid54').className = classname;
		document.getElementById('grid55').className = classname;
		document.getElementById('grid56').className = classname;
		document.getElementById('grid64').className = classname;
		document.getElementById('grid65').className = classname;
		document.getElementById('grid66').className = classname;
		break;

	case 6:
		document.getElementById('grid47').className = classname;
		document.getElementById('grid48').className = classname;
		document.getElementById('grid49').className = classname;
		document.getElementById('grid57').className = classname;
		document.getElementById('grid58').className = classname;
		document.getElementById('grid59').className = classname;
		document.getElementById('grid67').className = classname;
		document.getElementById('grid68').className = classname;
		document.getElementById('grid69').className = classname;
		break;

	case 7:
		document.getElementById('grid71').className = classname;
		document.getElementById('grid72').className = classname;
		document.getElementById('grid73').className = classname;
		document.getElementById('grid81').className = classname;
		document.getElementById('grid82').className = classname;
		document.getElementById('grid83').className = classname;
		document.getElementById('grid91').className = classname;
		document.getElementById('grid92').className = classname;
		document.getElementById('grid93').className = classname;
		break;

	case 8:
		document.getElementById('grid74').className = classname;
		document.getElementById('grid75').className = classname;
		document.getElementById('grid76').className = classname;
		document.getElementById('grid84').className = classname;
		document.getElementById('grid85').className = classname;
		document.getElementById('grid86').className = classname;
		document.getElementById('grid94').className = classname;
		document.getElementById('grid95').className = classname;
		document.getElementById('grid96').className = classname;
		break;

	case 9:
		document.getElementById('grid77').className = classname;
		document.getElementById('grid78').className = classname;
		document.getElementById('grid79').className = classname;
		document.getElementById('grid87').className = classname;
		document.getElementById('grid88').className = classname;
		document.getElementById('grid89').className = classname;
		document.getElementById('grid97').className = classname;
		document.getElementById('grid98').className = classname;
		document.getElementById('grid99').className = classname;
		break;

	default:
		rc = false;
		break;
	}
}

function check(n1, n2, n3, n4, n5, n6, n7, n8, n9)
{
	var count = new Array(0, 0, 0, 0, 0, 0, 0, 0 ,0, 0);
	count[n1]++;
	count[n2]++;
	count[n3]++;
	count[n4]++;
	count[n5]++;
	count[n6]++;
	count[n7]++;
	count[n8]++;
	count[n9]++;

	for (i = 1; i <= 9; i++)
	{
		if (count[i] > 1)
			return false;
	}

	return true;
}
